﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL
{
    //Promotions 促销
    public partial class P_Advertis_Payment
    {

        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from P_Advertis_Payment");
            strSql.Append(" where ");
            strSql.Append(" Id = @Id  ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RBS.Core.Model.P_Advertis_Payment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into P_Advertis_Payment(");
            strSql.Append("HotelId,UserId,CreateTime,DeedType,DeedStatus,Denomination,ValidStartTime,ValidEndTime,Quantity,Crowd,AccessRules,UseRules,IsCrossHotel");
            strSql.Append(") values (");
            strSql.Append("@HotelId,@UserId,@CreateTime,@DeedType,@DeedStatus,@Denomination,@ValidStartTime,@ValidEndTime,@Quantity,@Crowd,@AccessRules,@UseRules,@IsCrossHotel");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@DeedType", SqlDbType.Int,4) ,            
                        new SqlParameter("@DeedStatus", SqlDbType.Int,4) ,            
                        new SqlParameter("@Denomination", SqlDbType.Int,4) ,            
                        new SqlParameter("@ValidStartTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@ValidEndTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Quantity", SqlDbType.Int,4) ,            
                        new SqlParameter("@Crowd", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@AccessRules", SqlDbType.NVarChar,400) ,            
                        new SqlParameter("@UseRules", SqlDbType.NVarChar,400) ,            
                        new SqlParameter("@IsCrossHotel", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.HotelId;
            parameters[1].Value = model.UserId;
            parameters[2].Value = model.CreateTime;
            parameters[3].Value = model.DeedType;
            parameters[4].Value = model.DeedStatus;
            parameters[5].Value = model.Denomination;
            parameters[6].Value = model.ValidStartTime;
            parameters[7].Value = model.ValidEndTime;
            parameters[8].Value = model.Quantity;
            parameters[9].Value = model.Crowd;
            parameters[10].Value = model.AccessRules;
            parameters[11].Value = model.UseRules;
            parameters[12].Value = model.IsCrossHotel;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBS.Core.Model.P_Advertis_Payment model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update P_Advertis_Payment set ");

            strSql.Append(" HotelId = @HotelId , ");
            strSql.Append(" UserId = @UserId , ");
            strSql.Append(" CreateTime = @CreateTime , ");
            strSql.Append(" DeedType = @DeedType , ");
            strSql.Append(" DeedStatus = @DeedStatus , ");
            strSql.Append(" Denomination = @Denomination , ");
            strSql.Append(" ValidStartTime = @ValidStartTime , ");
            strSql.Append(" ValidEndTime = @ValidEndTime , ");
            strSql.Append(" Quantity = @Quantity , ");
            strSql.Append(" Crowd = @Crowd , ");
            strSql.Append(" AccessRules = @AccessRules , ");
            strSql.Append(" UseRules = @UseRules , ");
            strSql.Append(" IsCrossHotel = @IsCrossHotel  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@DeedType", SqlDbType.Int,4) ,            
                        new SqlParameter("@DeedStatus", SqlDbType.Int,4) ,            
                        new SqlParameter("@Denomination", SqlDbType.Int,4) ,            
                        new SqlParameter("@ValidStartTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@ValidEndTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Quantity", SqlDbType.Int,4) ,            
                        new SqlParameter("@Crowd", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@AccessRules", SqlDbType.NVarChar,400) ,            
                        new SqlParameter("@UseRules", SqlDbType.NVarChar,400) ,            
                        new SqlParameter("@IsCrossHotel", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.HotelId;
            parameters[2].Value = model.UserId;
            parameters[3].Value = model.CreateTime;
            parameters[4].Value = model.DeedType;
            parameters[5].Value = model.DeedStatus;
            parameters[6].Value = model.Denomination;
            parameters[7].Value = model.ValidStartTime;
            parameters[8].Value = model.ValidEndTime;
            parameters[9].Value = model.Quantity;
            parameters[10].Value = model.Crowd;
            parameters[11].Value = model.AccessRules;
            parameters[12].Value = model.UseRules;
            parameters[13].Value = model.IsCrossHotel;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from P_Advertis_Payment ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from P_Advertis_Payment ");
            strSql.Append(" where ID in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBS.Core.Model.P_Advertis_Payment GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id, HotelId, UserId, CreateTime, DeedType, DeedStatus, Denomination, ValidStartTime, ValidEndTime, Quantity, Crowd, AccessRules, UseRules, IsCrossHotel  ");
            strSql.Append("  from P_Advertis_Payment ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;


            RBS.Core.Model.P_Advertis_Payment model = new RBS.Core.Model.P_Advertis_Payment();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["HotelId"].ToString() != "")
                {
                    model.HotelId = int.Parse(ds.Tables[0].Rows[0]["HotelId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DeedType"].ToString() != "")
                {
                    model.DeedType = int.Parse(ds.Tables[0].Rows[0]["DeedType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DeedStatus"].ToString() != "")
                {
                    model.DeedStatus = int.Parse(ds.Tables[0].Rows[0]["DeedStatus"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Denomination"].ToString() != "")
                {
                    model.Denomination = int.Parse(ds.Tables[0].Rows[0]["Denomination"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ValidStartTime"].ToString() != "")
                {
                    model.ValidStartTime = DateTime.Parse(ds.Tables[0].Rows[0]["ValidStartTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ValidEndTime"].ToString() != "")
                {
                    model.ValidEndTime = DateTime.Parse(ds.Tables[0].Rows[0]["ValidEndTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Quantity"].ToString() != "")
                {
                    model.Quantity = int.Parse(ds.Tables[0].Rows[0]["Quantity"].ToString());
                }
                model.Crowd = ds.Tables[0].Rows[0]["Crowd"].ToString();
                model.AccessRules = ds.Tables[0].Rows[0]["AccessRules"].ToString();
                model.UseRules = ds.Tables[0].Rows[0]["UseRules"].ToString();
                if (ds.Tables[0].Rows[0]["IsCrossHotel"].ToString() != "")
                {
                    model.IsCrossHotel = int.Parse(ds.Tables[0].Rows[0]["IsCrossHotel"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM P_Advertis_Payment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM P_Advertis_Payment ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        } 
        #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *,(select count(AP_Id) from  P_Advertis_Batch b where  AP_Id= p.id) as  Gift ");
            strSql.Append(",  (select count(AP_Id) from  P_Advertis_Batch b where  AP_Id= p.id and ABStatus=3) as  Used");
            strSql.Append("  FROM P_Advertis_Payment p"); 
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        public DataSet GetListByUser(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" select  p.*,b.Id as batchId,batchno,ABStatus,ALStatus, 
                             (select count(AP_Id) from  P_Advertis_Batch b where  AP_Id= p.id) as  Gift,
                              (select count(AP_Id) from  P_Advertis_Batch b where  AP_Id= p.id and ABStatus=3) as  Used,
                             (select COUNT(1) from P_Advertis_Batch  where AP_Id=b.AP_Id ) as num
                             from P_Advertis_Payment p
                             left join P_Advertis_Batch b on p.Id=b.AP_Id
                             left join P_Advertis_List l on b.Id=l.AB_Id"); 

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        #endregion  ExtensionMethod



        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool UpdateStatus(int id, int status)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update P_Advertis_Payment set ");  
            strSql.Append(" DeedStatus = @DeedStatus  "); 
            strSql.Append(" where Id=@Id "); 
            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,      
                        new SqlParameter("@DeedStatus", SqlDbType.Int,4)    
            }; 
            parameters[0].Value = id;
            parameters[1].Value = status; 
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }
}

